<?php
/**
 * @version 2007.07.02
 *
 * This is the home page for the site template.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
ReqLib('/lib/private/NeededItem.php');
ReqLib('/lib/private/Item.php');
ReqLib('/lib/private/Orphanage.php');
require_once('calendar/classes/tc_calendar.php');

/**
 * Defines preload page functionality;
 * this happens after DefineParams,
 * but before anything else.
 */
function DefinePreload() {//
  // Insert page preload functionality here.

  switch($_GET['Action'])
  {

  case 'Add New Needed Item':
	$ni = new NeededItem();

	if($_POST['ItemName'] != 'Select Item Name' && $_POST['OrphanageName'] != 'Select Orphanage Name'){

		if(!$ni->Create(Item::GetItemIdByItemName($_POST['ItemName']),Orphanage::GetOrphanageIdByOrphanageName($_POST['OrphanageName']))){
		//creation of the user failed
				?>
					<script language="javascript">
						alert("Attempt to Add New Needed Item Failed");
					</script>
				<?
		}
		else {
			$ni->ItemId = Item::GetItemIdByItemName($_POST['ItemName']);
			$ni->OrphanageId = Orphanage::GetOrphanageIdByOrphanageName($_POST['OrphanageName']);
			$ni->NeededQuantity = $_POST['NeededQuantity'];
			$ni->QuantitySent = $_POST['QuantitySent'];
			$ni->DateRequested = $_POST['DateRequested'];
			$ni->NeededNotes = $_POST['NeededNotes'];

			$ni->Save();

			$neededItemId = $ni->NeededItemId;

		//creation of user worked

?>
	<script language="javascript">
		if(window.confirm("New Needed Item information has been added successfully.\nDo you want to add more Needed Item?"))
			document.location.replace("neededItems.php");
		else
			document.location.replace("account.php");
	</script>
<?
		}
	}

	else{
	?>
		<script language="javascript">
			alert("Item Name or Orphanage Name is Invalid.\nEnter Valid Name");
		</script>
	<?
	}

  break;
  }

}

/**
 * Defines page parameters.
 *
 * @param PageParams $p
 */
function DefineParams(&$p) {
  // Define page parameters here.
}

/**
 * Defines custom header content.
 *
 */
function DefineHeader() {
  // Custom HTML info goes here,
  // just before the closing </head> tag.
?>
	<script language="javascript" src="calendar/calendar.js"></script>
<?

}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>


<h1>Add New Needed Item</h1>
<p>
  <a href="<?=_webroot('/') ?>">Home</a>.
</p>
<p>
  <a href="account.php">My Account</a>.
</p>
<p>
  <a href="logout.php">Logout</a>.
</p>
<center>
<br />
<? if($_GET['Action'] == 'Add New Needed Item') echo $GLOBALS['RegPassFail'] ?>
<br />
<form name="AddNeededItem" action="<?=_webroot('/AddNeededItem.php?Action=Add New Needed Item') ?>"  method="POST">
<table border="2">
<tr>
	<td>Item Name:</td>
	<td>
		<select name="ItemName">
			<option>Select Item Name</option>
<?
	foreach(Item::GetItems() as $i)
		print('<option>'.$i->Name.'</option>');
?>
		</select>
	</td>
</tr>
<tr>
	<td>Orphanage Name:</td>
	<td>
		<select name="OrphanageName">
			<option>Select Orphanage Name</option>
<?
		foreach(Orphanage::GetOrphanages() as $o)
		print('<option>'.$o->Name.'</option>');

?>
		</select>
	</td>
</tr>
<tr>
	<td>Needed Quantity:</td>
	<td><input type = "Text" name ="NeededQuantity" value = "<?php echo $_POST['NeededQuantity'];?>" /></td>
</tr>
<tr>
	<td>Date Requested:</td>
	<td>
	<?
	$myCalendar = new tc_calendar("DateRequested", true, false);
	$myCalendar->setIcon("calendar/images/iconCalendar.gif");
	//$myDate = explode("-",$_POST['DateRequested']);
	$myCalendar->setDate(date('d'), date('m'), date('Y'));
	$myCalendar->setPath("calendar/");
	$myCalendar->setYearInterval(2000, 2015);
	$myCalendar->dateAllow('1950-05-13', '2015-03-01');
	$myCalendar->setDateFormat('j F Y');
	$myCalendar->setAlignment('left', 'bottom');
	$myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
	$myCalendar->writeScript();
	?>
	</td>
</tr>
<tr>
	<td>Quantity Sent: </td>
	<td><input type = "Text" name ="QuantitySent" value="<?php echo $_POST['QuantitySent'];?>" / ></td>
</tr>
<tr>
	<td>Anything else we should know?</td>
	<td><input type="Text" name = "NeededNotes" value="<?php echo $_POST['NeededNotes']?>" /></td>
</tr>
<tr >
	<td align="center"><input type="submit" value="Add New Needed Item" /></td>
	<td align="center"><input type="button" value="Back" onClick="history.go(-1)" /></td>
</tr>
</table>
</form>
</center>

<?

}

// Launches the page; without this, nothing happens.
require_once('_loader.php');